<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * group.php
 * @author ranuandra
 *
 * 12 Sep 2011 08:32:38
 *
 * 
 */

class Controller_Group extends Kohana_Mapi
{
	public function action_view()
	{
		$this->auto_render = false;

		$location = ORM::factory('group')->find_all()->as_array();
		$hasil = array();
		foreach($location as $data)
		{
			$hasil[] = $data->as_array();
		}
		echo json_encode(array("success" => true, "message" => "Loaded data", "group" => $hasil));		
	}
	
	public function action_create()
	{
		$this->auto_render = false;
		$datas = json_decode(stripcslashes($_POST['group']), true);
		

		$dataarray = false;
		$single = array();
		$output = array();
		$hasil = array();

			foreach($datas as $data)
			{
				if (is_array($data)) 
				{
					try {
					//$post = Validation::factory($_POST);
						$group = ORM::factory('group');
						$group->create_group($data);
						//Request::current()->redirect(URL::base().'customer');
						//array_push($array, $var)
						$single = array_merge(array("id"=>$group->id), $data);
						//array_push($single, $data);
						array_push($hasil, $single);
						//echo json_encode(array("success" => true, "message" => "Loaded data", "ratebeli" => $hasil));
	
					}
					catch(ORM_Validation_Exception $e)
					{
						$errors = $e->errors('models');
						print_r($errors);
					}
					$dataarray = true;
				}

			}	
			if($dataarray == false)
			{
					try {
					//$post = Validation::factory($_POST);
						$group = ORM::factory('group');
						$group->create_group($datas);
						//Request::current()->redirect(URL::base().'customer');
						//array_push($array, $var)
						$single = array_merge(array("id"=>$group->id), $datas);
						//array_push($single, $data);
						array_push($hasil, $single);
						//echo json_encode(array("success" => true, "message" => "Loaded data", "ratebeli" => $hasil));
	
					}
					catch(ORM_Validation_Exception $e)
					{
						$errors = $e->errors('models');
						print_r($errors);
					}				
			}

		//exit;

		echo json_encode(array("success" => true, "message" => "Loaded data", "group" => $hasil));		
	}
}